home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / Miro_Downloader.exe / BitTorrent / Storage.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-11-12  |  6.7 KB  |  236 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. from sha import sha
  5. from bisect import bisect_right
  6.  
  7. class Storage:
  8.     
  9.     def __init__(self, files, open, exists, getsize):
  10.         self.ranges = []
  11.         total = 0x0L
  12.         so_far = 0x0L
  13.         for file, length in files:
  14.             if length != 0:
  15.                 self.ranges.append((total, total + length, file))
  16.                 total += length
  17.                 if exists(file):
  18.                     l = getsize(file)
  19.                     if l > length:
  20.                         l = length
  21.                     
  22.                     so_far += l
  23.                 
  24.             exists(file)
  25.             if not exists(file):
  26.                 open(file, 'wb').close()
  27.                 continue
  28.         
  29.         self.begins = [ i[0] for i in self.ranges ]
  30.         self.total_length = total
  31.         self.handles = { }
  32.         self.whandles = { }
  33.         self.tops = { }
  34.         for file, length in files:
  35.             if exists(file):
  36.                 l = getsize(file)
  37.                 self.tops[file] = l
  38.                 continue
  39.             None if l != length else []
  40.             self.handles[file] = open(file, 'wb+')
  41.             self.whandles[file] = 1
  42.         
  43.  
  44.     
  45.     def was_preallocated(self, pos, length):
  46.         for file, begin, end in self._intervals(pos, length):
  47.             if self.tops.get(file, 0) < end:
  48.                 return False
  49.                 continue
  50.         
  51.         return True
  52.  
  53.     
  54.     def set_readonly(self):
  55.         for file in self.whandles.keys():
  56.             old = self.handles[file]
  57.             old.flush()
  58.             old.close()
  59.             self.handles[file] = open(file, 'rb')
  60.         
  61.  
  62.     
  63.     def get_total_length(self):
  64.         return self.total_length
  65.  
  66.     
  67.     def _intervals(self, pos, amount):
  68.         r = []
  69.         stop = pos + amount
  70.         p = bisect_right(self.begins, pos) - 1
  71.         while p < len(self.ranges) and self.ranges[p][0] < stop:
  72.             (begin, end, file) = self.ranges[p]
  73.             r.append((file, max(pos, begin) - begin, min(end, stop) - begin))
  74.             p += 1
  75.         return r
  76.  
  77.     
  78.     def files_in_range(self, pos, amount):
  79.         return [ file for file, pos, end in self._intervals(pos, amount) ]
  80.  
  81.     
  82.     def read(self, pos, amount):
  83.         r = []
  84.         for file, pos, end in self._intervals(pos, amount):
  85.             h = self.handles[file]
  86.             h.seek(pos)
  87.             r.append(h.read(end - pos))
  88.         
  89.         return ''.join(r)
  90.  
  91.     
  92.     def write(self, pos, s):
  93.         total = 0
  94.         for file, begin, end in self._intervals(pos, len(s)):
  95.             if not self.whandles.has_key(file):
  96.                 self.handles[file].close()
  97.                 self.handles[file] = open(file, 'rb+')
  98.                 self.whandles[file] = 1
  99.             
  100.             h = self.handles[file]
  101.             h.seek(begin)
  102.             h.write(s[total:total + end - begin])
  103.             total += end - begin
  104.         
  105.  
  106.     
  107.     def close(self):
  108.         for h in self.handles.values():
  109.             h.close()
  110.         
  111.  
  112.  
  113.  
  114. def lrange(a, b, c):
  115.     r = []
  116.     while a < b:
  117.         r.append(a)
  118.         a += c
  119.     return r
  120.  
  121. from fakeopen import FakeOpen
  122.  
  123. def test_Storage_simple():
  124.     f = FakeOpen()
  125.     m = Storage([
  126.         ('a', 5)], f.open, f.exists, f.getsize)
  127.     if not f.files.keys() == [
  128.         'a']:
  129.         raise AssertionError
  130.     m.write(0, 'abc')
  131.     if not m.read(0, 3) == 'abc':
  132.         raise AssertionError
  133.     m.write(2, 'abc')
  134.     if not m.read(2, 3) == 'abc':
  135.         raise AssertionError
  136.     m.write(1, 'abc')
  137.     if not m.read(0, 5) == 'aabcc':
  138.         raise AssertionError
  139.  
  140.  
  141. def test_Storage_multiple():
  142.     f = FakeOpen()
  143.     m = Storage([
  144.         ('a', 5),
  145.         ('2', 4),
  146.         ('c', 3)], f.open, f.exists, f.getsize)
  147.     x = f.files.keys()
  148.     x.sort()
  149.     if not x == [
  150.         '2',
  151.         'a',
  152.         'c']:
  153.         raise AssertionError
  154.     m.write(3, 'abc')
  155.     if not m.read(3, 3) == 'abc':
  156.         raise AssertionError
  157.     m.write(5, 'ab')
  158.     if not m.read(4, 3) == 'bab':
  159.         raise AssertionError
  160.     m.write(3, 'pqrstuvw')
  161.     if not m.read(3, 8) == 'pqrstuvw':
  162.         raise AssertionError
  163.     m.write(3, 'abcdef')
  164.     if not m.read(3, 7) == 'abcdefv':
  165.         raise AssertionError
  166.  
  167.  
  168. def test_Storage_zero():
  169.     f = FakeOpen()
  170.     Storage([
  171.         ('a', 0)], f.open, f.exists, f.getsize)
  172.     if not f.files == {
  173.         'a': [] }:
  174.         raise AssertionError
  175.  
  176.  
  177. def test_resume_zero():
  178.     f = FakeOpen({
  179.         'a': '' })
  180.     Storage([
  181.         ('a', 0)], f.open, f.exists, f.getsize)
  182.     if not f.files == {
  183.         'a': [] }:
  184.         raise AssertionError
  185.  
  186.  
  187. def test_Storage_with_zero():
  188.     f = FakeOpen()
  189.     m = Storage([
  190.         ('a', 3),
  191.         ('b', 0),
  192.         ('c', 3)], f.open, f.exists, f.getsize)
  193.     m.write(2, 'abc')
  194.     if not m.read(2, 3) == 'abc':
  195.         raise AssertionError
  196.     x = f.files.keys()
  197.     x.sort()
  198.     if not x == [
  199.         'a',
  200.         'b',
  201.         'c']:
  202.         raise AssertionError
  203.     if not len(f.files['a']) == 3:
  204.         raise AssertionError
  205.     if not len(f.files['b']) == 0:
  206.         raise AssertionError
  207.  
  208.  
  209. def test_Storage_resume():
  210.     f = FakeOpen({
  211.         'a': 'abc' })
  212.     m = Storage([
  213.         ('a', 4)], f.open, f.exists, f.getsize)
  214.     if not f.files.keys() == [
  215.         'a']:
  216.         raise AssertionError
  217.     if not m.read(0, 3) == 'abc':
  218.         raise AssertionError
  219.  
  220.  
  221. def test_Storage_mixed_resume():
  222.     f = FakeOpen({
  223.         'b': 'abc' })
  224.     m = Storage([
  225.         ('a', 3),
  226.         ('b', 4)], f.open, f.exists, f.getsize)
  227.     x = f.files.keys()
  228.     x.sort()
  229.     if not x == [
  230.         'a',
  231.         'b']:
  232.         raise AssertionError
  233.     if not m.read(3, 3) == 'abc':
  234.         raise AssertionError
  235.  
  236.